package summary;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/13 15:13
 * https://leetcode-cn.com/problems/print-words-vertically/
 */
public class Title1324 {

    public static List<String> printVertically(String s) {
        String[] split = s.split(" ");
        int sum = 0;
        for (int i = 0; i < split.length; i++) {
            sum += split[i].length();
        }
        List<String> result = new ArrayList<>();
        int count = 0, start = 0;
        while (count < sum) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.length; i++) {
                if (start < split[i].length()) {
                    sb.append(split[i].charAt(start));
                    count++;
                }else{
                    sb.append(' ');
                }
            }
            start++;
            String string = sb.toString();
            int end = string.length();
            for (int i = string.length()-1; i >=0 ; i--) {
                if (' ' == string.charAt(i)) {
                    end--;
                }else{
                    break;
                }
            }
            result.add(string.substring(0, end));
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> contest_is_coming = printVertically("CONTEST IS COMING");
    }
}
